﻿using System;
using System.Text;
using Win32;

namespace SimpleTerminal.TerminalControl
{
    partial class Terminal
    {
        protected TerminalContainer container;
        protected TerminalContainer rowsMain;
        protected int screenLineStart = 0;

        public int ScreenLineStart
        {
            get => screenLineStart;
            set
            {
                screenLineStart = value;
                //PaintEvent(PaintEvents.ScrollBarChanged);
                //PaintEvent(TEvent.ScrollBarChanged);
            }
        }

        private void InitializeRows()
        {
            rowsMain = new TerminalContainer(300, 80, 0);
            //rowsMain.SetForegroundColor
            container = rowsMain;
            rowsMain.OnCountChanged += new TerminalContainer.CountChangedHandler(OnCountChanged);
        }

        private void OnCountChanged(int count)
        {
            //lock (_drawLock)
            // {
            //     if (rows.Maximum >= this.GetDisplayRows())
            //    {
            //         screenLineStart = rows.ConsoleOffset;
            //      }
            //      }
            //  }
        }

        public void SetCursorVisible(bool visible)
        {
            //if (visible)
            //    User32.ShowCaret(Handle);
            // else
            //    User32.HideCaret(Handle);
            this.cursorVisible = visible;
        }

        public void Input(char[] buffer)
        {
            analyzer.Write(Encoding.UTF8.GetBytes(buffer));
        }

        public void Input(byte[] buffer)
        {
            analyzer.Write(buffer);
        }

        public void WriteTab()
        {
            int c = 8 - ((container.CursorIndex) % 8);
            container.CursorIndex += c;
        }



        public void Write(char character)
        {
            container.Write(character);
        }

        public void DeleteCharacters(int len)
        {
            container.DeleteCharacters(len);
        }

        public void MoveCursorOffset(int offset)
        {

            container.CursorIndex = offset;
        }
        public void MoveCursorEraseCharacters(int offset)
        {
            container.MoveCursorEraseCharacters(offset);
        }


        /// <summary>
        /// 设置可滚动区域
        /// </summary>
        /// <param name="top"></param>
        /// <param name="bottom"></param>
        public void SetScrollingRegion(int top, int bottom)
        {
            container.SetScrollingRegion(top, bottom);
        }


        public void Set1049Model(bool model)
        {
            Invoke((EventHandler)delegate
            {
                selectRegion.Reset();
                //DoPaint();
                if (model)
                {
                    //saveScreenLineStart = screenLineStart;
                    ScreenLineStart = 0;
                    container = new TerminalContainer(displayColumns, displayRows, 0);
                    UpdateScrollBar();

                }
                else
                {
                    container = null;
                    container = rowsMain;
                    container.Height = displayRows;
                    container.ConsoleWidth = displayColumns;
                    ScreenLineStart = container.ConsoleOffset;
                    UpdateScrollBar();

                }

            });
        }

        public void InsertBlankLine(int n)
        {
            container.InsertBlankLine(n);
        }
        public void EraseCharacters(int n)
        {
            container.EraseCharacters(n);
        }

        public void ReverseVideo(bool val)
        {
            this.container.ReverseVideo(val);
        }

        public void ResetCursor(bool onlyAttrs)
        {
            container.ResetCursor(onlyAttrs);

        }

        public void InsertBlankCharceters(int n)
        {
            container.InsertBlankCharceters(n);
        }

        public void DelectLine(int n)
        {
            this.container.DelectLine(n);
        }

        public void Translation(int top)
        {
            this.container.Translation(top);
        }

        public void MoveCursorLine(int line)
        {
            this.container.CursorLineIndex = line;
        }

        public int ScrollBackSize
        {
            set
            {
                if (value != container.ScrollBackSize)
                {
                    if (value < container.ConsoleOffset)
                    {
                        screenLineStart -= Math.Min(screenLineStart, container.ConsoleOffset - value);
                        container.ScrollBackSize = value;
                        UpdateScrollBar();
                        container.InvalidAbs(screenLineStart, displayRows);
                        Invalidate();
                    }
                    else
                    {
                        container.ScrollBackSize = value;
                        UpdateScrollBar();
                    }
                }
            }
        }
        public void PostCursorOffset(int offset)
        {
            if (container.CursorIndex == 0 && offset < 0)
                return;
            container.CursorIndex += offset;
        }

        public void PostCursorLine(int line)
        {
            this.container.PostCursorLine(line);
        }
        public void ReverseLinefeed()
        {
            this.container.ReverseLinefeed();
        }

        public void MovetToNextLine()
        {
            container.ScrollToNext();
            //container.CursorIndex = 0;
        }

        public void RemoveCursorToEnd()
        {
            this.container.RemoveCursorToEnd();
        }

        public void BeepPlay()
        {
            System.Media.SystemSounds.Beep.Play();
        }


        public void Clear()
        {
            EraseDisplay(3);
        }


        public void EraseDisplay(int type = 1)
        {
            container.EraseDisplay(type);
        }

        public void EraseLine(int type = 1)
        {
            container.EraseLine(type);
        }
        public void ClearAll()
        {
            container.Reset();
            selectRegion.Reset();
            ScreenLineStart = 0;
            UpdateScrollBar();
            Invalidate();
        }

        public void ClearScrollBack()
        {

            int oldOffset = container.ScreenOffset;
            container.ClearScrollBack();
            selectRegion.Reset();
            UpdateScrollBar();
            if (oldOffset > 0)
            {
                ScrollTo(-oldOffset, 0);
            }
            InvalidatePaint();
        }

        public void SetForeground(int color)
        {
            this.container.SetForegroundColor(color);
        }
        public void SetForegroundRgb(byte r, byte g, byte b)
        {
            this.container.SetForegroundRgb(r, g, b);
        }
        public void SetBackground(int color)
        {
            this.container.SetBackgroundColor(color);
        }
        public void SetBackgroundRgb(byte r, byte g, byte b)
        {
            this.container.SetBackgroundRgb(r, g, b);
        }

        public void ResetForeground()
        {
            container.ResetForeground();
        }

        public void ResetBackground()
        {
            this.container.ResetBackground();
        }

        public void SetHeightLight(bool value)
        {
            container.HeightLight = value;

        }
        public void SetCharacterStyle(int style)
        {
            this.container.SetCharacterStyle(style);
        }

        public void UnsetCharacterStyle(int style)
        {
            this.container.UnsetCharacterStyle(style);
        }

        public void SaveCurrentState()
        {
            this.container.SaveCursorState();
        }
        public void RestoreCurrentState()
        {
            this.container.RestoreCursorState();
        }

    }
}
